// pages/login/login.js
const app = getApp()
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
// import {
//   Room_cache_image,
// } from '../../utils/cache_image';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    circleSpin: false,
    back_image: 'https://img.xunyinjiaoyou.com/static/xiaochengxu/shouye/new_image/logonimg.png',
  },
  toabout_link() {
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/agreement"
    })
  },
  toabout() {
    // wx.navigateTo({
    //   url: '/pages/privacy/privacy',
    // })
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/privacy"
    })
  },
  tophone() {
    if (this.data.checked) {
      wx.navigateTo({
        url: '/pages/phone_login/phone_login',
      })
    } else {
      wx.showToast({
        title: "未同意 《 用户协议 》",
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },
  //微信登录
  wx_login() {
    if (!this.data.checked) {
      wx.showToast({
        title: "未同意 《 用户协议 》",
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return
    }
    this.setData({
      circleSpin: true,
    })
    app.user_info('1').then((res) => {
      if (res) {
        wx.showToast({
          title: '登录成功',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }, 1000);
      } else {
        wx.showToast({
          title: "授权失败",
          icon: 'none',
          duration: 2000 //持续的时间
        })
      }
      this.setData({
        circleSpin: false,
      })
    }, (error) => {
      console.log('数据请求失败：' + error)
    })
  },
  checked: function (e) {
    var check = this.data.checked;
    if (check) {
      this.data.checked = false;
    } else {
      this.data.checked = true;
    }
    this.setData({
      "checked": this.data.checked,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _ = this;
    wx.login({ //获取code
      success: res => {
        console.log(res);
        wx.setStorageSync('code', res.code)
        let params = {
          code: res.code,
          type: "2",
        };
        axios.axios({
          url: "/index/getOpenId",
          data: params
        }).then((res) => {
          wx.setStorageSync('unionid', res.data.result.unionid)
        })
      }
    });
    if (options.SignIn == 'index') {
      wx.navigateTo({
        url: '/pages/phone_login/phone_login?SignIn=' + 'index',
      })
    }
    let Room_image = {};
    wx.setStorageSync('Room_image', Room_image);
    // Room_cache_image('https://img.xunyinjiaoyou.com/static/xiaochengxu/shouye/new_image/logonimg.png', 'login_backage').then((back_image) => {
    //   _.setData({
    //     back_image: back_image
    //   })
    // }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
